package com._520it.crm.web.controller;

import com._520it.crm.domain.Department;
import com._520it.crm.domain.Employee;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.DepartmentQueryObject;
import com._520it.crm.query.EmployeeQueryObject;
import com._520it.crm.service.IDepartmentService;
import com._520it.crm.service.IEmployeeService;
import com._520it.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by ASUS on 2017/10/9.
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("")
    public String index(){
        return "department";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(DepartmentQueryObject qo){

        return departmentService.query(qo);
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(Department department){
        AjaxResult result =null;
        try{
            departmentService.insert(department);
            result = new AjaxResult("新增成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,"新增失败");
        }
        return result;
    }
    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult update(Department department){
        AjaxResult result =null;
        try{
            departmentService.updateByPrimaryKey(department);
            result = new AjaxResult("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,"更新失败");
        }
        return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult result =null;
        try{
            departmentService.deleteByPrimaryKey(id);
            result = new AjaxResult("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new AjaxResult(false,"删除失败");
        }
        return result;
    }



    @RequestMapping("/queryDepartmentList")
    @ResponseBody
    public List<Department> queryDepartmentList(){
        List<Department> result =null;
        result =departmentService.selectAll();
        return  result;
    }


}
